package com.seo.service.proxy.recharge.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.seo.function.PageTool;
import com.seo.mapper.bill.proxy.ProxyTransactionMapper;
import com.seo.mapper.proxy.ProxyAccountMapper;
import com.seo.mapper.proxy.recharge.ProxyRechargeMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.proxy.recharge.sdk.ProxyRechargeService;
import com.seo.service.proxy.sdk.ProxyUserService;
import com.seo.utils.CustomUtil;
import com.seo.utils.DataUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("proxyRechargeService")
public class ProxyRechargeServiceImpl implements ProxyRechargeService {

	@Autowired
	private ProxyRechargeMapper proxyRechargeMapper;

	@Autowired
	private ProxyTransactionMapper proxyTransactionMapper;

	@Autowired
	private ProxyAccountMapper proxyAccountMapper;

	@Autowired
	private ProxyUserService proxyUserService;

	@Autowired
	private PageTool pageTool;

	@Override
	public ReturnBody getProxyRechargeDetailList(String tradeId, Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		
		String type = (String) params.get("type");
		if (StringUtils.isNotEmpty(type)) {
			String types = proxyRechargeMapper.getRechargeType(type, 5);
			if (StringUtils.isEmpty(types)) {
				log.error("[{}]-充值列表,充值类型[{}]不存在", tradeId, type);
				return rb.error("充值类型不存在");
			}
			params.put("types", types);
		}
		PageInfo<?> page = pageTool.getAutoPage(proxyRechargeMapper::getProxyRechargeDetailList, params);
		rb.put("page", page);
		return rb.success();
	}

	@Override
	public ReturnBody saveProxyRechargeDetail(String tradeId, Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		try {
			
			if (!params.containsKey("userId")) {
				return rb.error("代理用户不能为空");
			}
			Integer userId = (Integer) params.get("userId");
			if (userId == null || userId <= 0) {
				return rb.error("代理用户不能为空");
			}
			
			log.info("[{}]-查看用户是否存在", tradeId);
			if (!params.containsKey("userName")) {
				log.error("[{}]-用户名不能为空", tradeId);
				return rb.rollback("用户不能为空");
			}

			String userName = (String) params.get("userName");
			rb.run(proxyUserService.getProxyUserByName(userId, userName));
			if (!rb.isSuccess()) {
				return rb;
			}
			Integer proxyUserId = rb.get("proxyUserId", Integer.class);
			if (!params.containsKey("price")) {
				log.error("[{}]-充值金额不能为空", tradeId);
				return rb.rollback("充值金额不能为空");
			}

			BigDecimal price = DataUtil.getBigDecimal(params.get("price"));
			if (price.compareTo(BigDecimal.ZERO) <= 0) {
				return rb.error("充值金额不能小于0");
			}

			if (!params.containsKey("payMode")) {
				log.error("[{}]-支付类型不能为空", tradeId);
				return rb.rollback("支付类型不能为空");
			}
			String payMode = (String) params.get("payMode");
			String typeR = proxyRechargeMapper.getRechargeType(payMode, 1);
			if (StringUtils.isEmpty(typeR)) {
				log.error("[{}]-支付类型不存在", tradeId);
				return rb.rollback("支付类型不存在");
			}

			params.put("proxyUserId", proxyUserId);

			if (!params.containsKey("remark")) {
				params.put("remark", "手动充值");
			}

			log.info("[{}]-插入账单明细", tradeId);
			rb.run(saveProxyTransactionFlow(tradeId, params));
			if (!rb.isSuccess()) {
				return rb;
			}
			log.info("[{}]-增加用户余额，插入用户余额明细", tradeId);
			rb.run(saveProxyBalanceDetail(tradeId, rb.get("proxyTf", String.class, Object.class)));
			if (!rb.isSuccess()) {
				return rb;
			}

			log.info("[{}]-插入用户充值明细", tradeId);
			Map<String, Object> bd = rb.get("proxyBd", String.class, Object.class);
			params.put("balanceId", bd.get("id"));
			params.put("remark", bd.get("remark"));
			params.put("state", "01");
			params.put("type", payMode);
			int num = proxyRechargeMapper.saveProxyRechargeDetail(params);
			if (num == 0) {
				log.error("[{}]-插入充值明细-失败", tradeId);
				return rb.rollback();
			}
			return rb.success();
		} catch (Exception e) {
			log.error("异常-{}", e);
			return rb.rollback();
		} finally {
			rb.result();
		}
	}

	public ReturnBody saveProxyTransactionFlow(String tradeId, Map<String, Object> kd) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("userId", kd.get("userId"));
		params.put("proxyUserId", kd.get("proxyUserId"));
		params.put("amount", kd.get("price"));
		params.put("amountState", "01");
		params.put("remark", kd.get("remark"));
		params.put("state", "01");
		params.put("type", "recharge");
		int num = proxyTransactionMapper.saveProxyTransactionFlow(params);
		if (num == 0) {
			log.error("[{}]-插入代理客户账单-失败", tradeId);
			return rb.rollback();
		}
		params.put("code", CustomUtil.orderCode(3, (long) params.get("id"), (int) params.get("proxyUserId")));
		num = proxyTransactionMapper.updateProxyTransactionFlowCode(params);
		if (num == 0) {
			log.error("[{}]-插入代理客户账单-失败", tradeId);
			return rb.rollback();
		}
		log.info("[{}]-插入代理客户账单[{}]-成功", tradeId, params);
		rb.put("proxyTf", params);
		return rb.success();

	}

	public ReturnBody saveProxyBalanceDetail(String tradeId, Map<String, Object> proxyTf) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		params.put("proxyUserId", proxyTf.get("proxyUserId"));
		params.put("balance", proxyTf.get("amount"));
		params.put("type", "1");
		int num = proxyAccountMapper.updateProxyUserBalance(params);
		if (num == 0) {
			log.error("[{}]-更新代理用户余额-失败", tradeId);
			return rb.rollback();
		}
		params.put("userId", proxyTf.get("userId"));
		params.put("incomeState", "01");
		params.put("type", "recharge");
		params.put("remark", proxyTf.get("remark"));
		params.put("transactionId", proxyTf.get("id"));
		params.put("state", "01");
		num = proxyAccountMapper.saveProxyBalanceDetail(params);
		if (num == 0) {
			log.error("[{}]-保存代理用户余额明细-失败", tradeId);
			return rb.rollback();
		}
		rb.put("proxyBd", params);
		return rb.success();
	}

}
